package cn.hrc.filter;


import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebFilter(filterName = "MyFilter",urlPatterns = "/*")
public class MyFilter implements Filter {
    public MyFilter() {
        System.out.println("MyFilter ......");
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("进入到过滤器。。。");
        //向下转型
        HttpServletRequest req= (HttpServletRequest) servletRequest;
        HttpSession session = req.getSession();
        String servletPath = req.getServletPath();

        System.out.println("Session: " + session);
        System.out.println("请求路径\nServletPath: " + servletPath);

        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}
